package com.woniuxy.lrz.carlproject.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.RandomUtil;
import com.woniuxy.lrz.carlproject.common.Log;
import com.woniuxy.lrz.carlproject.common.Result;
import com.woniuxy.lrz.carlproject.common.util.OssTemplate;
import com.woniuxy.lrz.carlproject.controller.form.userform.*;
import com.woniuxy.lrz.carlproject.model.CarDetail;
import com.woniuxy.lrz.carlproject.model.ReceiveAddress;
import com.woniuxy.lrz.carlproject.model.User;
import com.woniuxy.lrz.carlproject.model.UserCar;
import com.woniuxy.lrz.carlproject.service.UserService;
import com.woniuxy.lrz.carlproject.service.dto.userdto.CarDetailDTO;
import com.woniuxy.lrz.carlproject.service.dto.userdto.LoginDTO;
import com.woniuxy.lrz.carlproject.service.dto.userdto.UserInfoDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "用户接口")
public class UserController {
    @Resource
    private UserService userService;


    //手机验证码
    @GetMapping("/getCheckCode")
    @ApiOperation("获取验证码")
    public Result getCheckCode(String phone){
        String checkCode = userService.getCheckCode(phone);
        return Result.success(checkCode);
    }

    //用户注册
    @PostMapping("/doRegister")
    @ApiOperation("用户注册")
    public Result userRegister(@RequestBody @Valid RegisterForm registerForm){
        userService.userRegister(registerForm);
        return Result.success("注册成功");
    }

    //用户登录
    @PostMapping("/doLogin")
    @Log("用户登录")
    @ApiOperation("用户登录")
    public Result userLogin(@RequestBody @Valid LoginForm loginForm){
        LoginDTO loginDTO = userService.userLogin(loginForm);
        return Result.success(loginDTO);
    }
    //查询个人详细信息
    @GetMapping("/getUserDetailInfo")
    @Log("查询个人详细信息")
    @ApiOperation("查询个人普通信息")
    public Result getUserDetailInfo(){
        User user = userService.getById(Integer.parseInt((String) StpUtil.getLoginId()));
        return Result.success(user);
    }

    //查询个人信息
    @GetMapping("/getUserInfo")
    @Log("查询个人普通信息")
    @ApiOperation("查询个人普通信息")
    public Result getUserInfo(){
        UserInfoDTO userInfoDTO = userService.getUserInfo();
        return Result.success(userInfoDTO);
    }

    //修改个人普通信息
    @PutMapping("/updateUserInfo")
    @Log("修改个人普通信息")
    @ApiOperation("修改个人普通信息")
    public Result updateUserInfo(@RequestBody ModifyInfoForm modifyInfoForm){
        userService.updateUserInfo(modifyInfoForm);
        return Result.success("修改信息成功");
    }

    //修改头像
    @PostMapping("/updateUserImage")
    @Log("修改个人头像")
    @ApiOperation("修改个人头像")
    public Result updateUserImage(@RequestParam("file") MultipartFile file) throws IOException {
//        @RequestParam("image")
        String s = RandomUtil.randomString(10);
        String img = (String)StpUtil.getLoginId()+s;
        String url = OssTemplate.fileUpload(img, file.getInputStream(), "woniuxy-81-3");
        log.info("url:{}",url);
        userService.updateUserImage(Integer.parseInt((String)StpUtil.getLoginId()),url);
        return Result.success("修改头像成功");
    }

    //添加收货地址
    @PostMapping("/addReceiveAddress")
    @Log("添加收货地址")
    @ApiOperation("添加收货地址")
    public Result addReceiveAddress(@RequestBody ReceiveAddressForm receiveAddressForm){
        userService.addReceiveAddress(receiveAddressForm);
        return Result.success("添加收货地址成功");
    }

    //修改收货地址
    @PutMapping("/updateReceiveAddress")
    @Log("修改收货地址")
    @ApiOperation("修改收货地址")
    public Result updateReceiveAddress(@RequestBody ReceiveAddressForm2 receiveAddressForm){
        userService.updateReceiveAddress(receiveAddressForm);
        return Result.success("修改收货地址成功");
    }

    //查询收货地址
    @GetMapping("/getReceiveAddress")
    @Log("获取收货地址")
    @ApiOperation("获取收货地址")
    public Result getReceiveAddress(){
        ArrayList<ReceiveAddress> addressList = userService.getReceiveAddress();
        return Result.success(addressList);
    }

    //删除收货地址
    @DeleteMapping("/deleteReceiveAddress")
    @Log("删除收货地址")
    @ApiOperation("删除收货地址")
    public Result deleteReceiveAddress(Integer receiveAddressId){
        userService.deleteReceiveAddress(receiveAddressId);
        return Result.success("删除收货地址成功");
    }

    //添加用户用车=============================================================
    @PostMapping("/addMyCar")
    @Log("添加用户用车")
    @ApiOperation("添加用户用车")
    public Result addMyCar(@RequestBody AddCarForm addCarForm){
        userService.addMyCar(addCarForm);
        return Result.success("添加用车成功");
    }
    //查询用户用车
    @GetMapping("/getMyCars")
    @Log("查询用户用车")
    @ApiOperation("查询用户用车")
    public Result getMyCars(){
        List<CarDetailDTO> cars = userService.getMyCars();
        return Result.success(cars);
    }
    //删除用户用车
    @DeleteMapping("/deleteCar")
    @Log("删除用户用车")
    @ApiOperation("删除用户用车")
    public Result deleteCar(Integer userCarId){
        userService.deleteCar(userCarId);
        return Result.success("删除用户用车成功");
    }
    //设为当前用车
    @GetMapping("/setCurrentCar")
    @Log("设为当前用车")
    @ApiOperation("设为当前用车")
    public Result setCurrentCar(Integer userCarId){
        userService.setCurrentCar(userCarId);
        return Result.success("设置成功");
    }
    //修改、完善车俩详情
    @PutMapping("/changeCarInfo")
    @Log("修改、完善车俩详情")
    @ApiOperation("修改、完善车俩详情")
    public Result changeCarInfo(@RequestBody ChangeCarForm changeCarForm){
        userService.changeCarInfo(changeCarForm);
        return Result.success("修改成功");
    }
    //根据车型详情id获取所有车型详情信息
    @GetMapping("/getAllCarDetailById")
    @Log("根据车型详情id获取所有车型详情信息")
    @ApiOperation("根据车型详情id获取所有车型详情信息")
    public Result getAllCarDetailById(Integer carDetailId){
        List<CarDetail> carDetails = userService.getAllCarDetailById(carDetailId);
        return Result.success(carDetails);
    }
}
